package com.ceba.test.modules.sign.helper;


import java.util.List;
import java.util.Map;

import com.ceba.base.web.response.IResult;
import com.ceba.test.base.configs.CebaConfig;
import com.ceba.test.base.enums.QrFileUploadFlagEnum;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ceba.test.base.sign.helper.CheckSignHelper;
import com.ceba.test.modules.admin.helper.QRCodeHelper;
import com.ceba.test.modules.filemanage.dto.WebSignParam;
import com.ceba.test.modules.sign.logicService.SignLogicService;
import com.ceba.test.modules.statistic.entity.ReportSign;
import com.ceba.test.modules.statistic.helper.CollectSignHelper;

import abc.ID;
import configs.context.SpringBeans;

public class SignHelper {
	private static Logger logger = LoggerFactory.getLogger(SignHelper.class);

	private static SignLogicService signLogicService = SpringBeans.getSignLogicService("signLogicService");
	
	//根据checkAllowSign获取不能签名的理由
	public static String getCheckAllowSignErr() throws Exception{
		JSONObject checkSign = CheckSignHelper.checkCanSign();
		return checkSign.getString("message");
	}
	
	
	/*
	 * 签名成功后台，更新份数和其他签名信息
	 * 
	 * 完整版/极简版/可视化签名签名
	 */
	public static void afterSignReport(String savePath,ReportSign  parm) throws Exception{
		if(parm.getNum() >0){
			QrFileUploadFlagEnum qrFileUploadFlag = QrFileUploadFlagEnum.DEFAULT_NONE;
			if("true".equals(CebaConfig.QR_PDF_NEED_UPLOAD) && "true".equals(CebaConfig.QRCODE)) {
				boolean ret = QRCodeHelper.sendQRPdfToServer(parm.getQrCodeFileId(), savePath);
				qrFileUploadFlag = ret ? QrFileUploadFlagEnum.UPLOAD_SUCCESS : QrFileUploadFlagEnum.UPLOAD_FAILURE;
			} else if ("async".equals(CebaConfig.QR_PDF_NEED_UPLOAD) && "true".equals(CebaConfig.QRCODE)) {
				qrFileUploadFlag = QrFileUploadFlagEnum.UPLOAD_FAILURE;
			} else {
				qrFileUploadFlag = QrFileUploadFlagEnum.DEFAULT_NONE;
			}
			parm.setQrFileUploadFlagEnum(qrFileUploadFlag);
			signLogicService.addSignRecord(parm);
			CollectSignHelper.updateSignCountToDb(parm);
			
		}
	}
	
	/**
	 * @param pdfFilePath pdf文件路径,
	 * @return 签名后的物理路径，
	 */
	public static IResult signFileInFM(String pdfFilePath, WebSignParam webSignParam, IResult result,String qrcodeFileId) {
		return signLogicService.signFileInFM( pdfFilePath, webSignParam, result,qrcodeFileId);
	}
	
	
	/**
	 * 插入骑缝章
	 * @param pdfFilePath
	 * @param webSignParam
	 * @param result
	 * @throws Exception 
	 */
	public static void insertQifeng(String pdfFilePath, WebSignParam webSignParam, IResult result) throws Exception {
		signLogicService.insertQifeng(pdfFilePath, webSignParam, result);
	}

	/**
	 * 合并首尾页
	 * @param pdfFilePath
	 * @param webSignParam
	 * @param result
	 * @throws Exception 
	 */
	public static void mergeFile(String pdfFilePath, WebSignParam webSignParam, IResult result) throws Exception {
		signLogicService.mergeFile(pdfFilePath, webSignParam, result);
	}
	
	/**
	 * 插入水印
	 * @return
	 * @throws Exception 
	 */
	public static void insertWaterStamp(String pdfFilePath,WebSignParam webSignParam,IResult result) throws Exception {
		//signLogicService.insertWaterStamp(pdfFilePath, webSignParam, result);
		signLogicService.insertWaterStampPages(pdfFilePath,webSignParam,result);
	}
	/**
	 * 插入其他签章的方法
	 * 
	 */
	public static void insertOtherSealStamps(String pdfFilePath, String outPath, List<String> otherStampIds,IResult result) {
		signLogicService.insertOtherSealStamps(pdfFilePath, outPath, otherStampIds,result);
	}

	/**
	 * 插入其他签章的方法-带自定义日期
	 *
	 */
	public static void insertOtherSealStampsWithCustomDate(String pdfFilePath, String outPath, List<String> otherStampIds, Map<String, String> customDateToContent, IResult result) {
		signLogicService.insertOtherSealStampsWithCustomDate(pdfFilePath, outPath, otherStampIds, customDateToContent,result);
	}


	/**
	 * 
	 * 数字签章插入单个关键字
	 * 先查找关键字
	 * 再定位盖章
	 */
	public static void insertSignSealStamps(String pdfFilePath, String outPath, List<String> signStampIds,IResult result) {
		signLogicService.insertSignSealStamps(pdfFilePath, outPath, signStampIds,result);
	}

	public static boolean qrKeyMatchNeedShowError(String inputPath, String outPath, JSONObject crJsonStamp, boolean isNoErrorNotNoramlMatchKey) {
		return signLogicService.qrKeyMatchNeedShowError(inputPath, outPath, crJsonStamp, isNoErrorNotNoramlMatchKey);
	}
}
